package com.yaowk.authc.listener;

import com.yaowk.authc.model.User;
import org.apache.shiro.SecurityUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @authc yaowk
 * 2017/4/29
 */
public class LoginSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        ServletContext application = httpSessionEvent.getSession().getServletContext();
        Integer total = (Integer) application.getAttribute("total".toString());
        Integer platformNum = (Integer) application.getAttribute(user.getPlatformId().toString());
        synchronized (total) {
            application.setAttribute(user.getPlatformId().toString(), --platformNum);
            application.setAttribute("total", --total);
        }
    }
}
